﻿using UnityEngine.Experimental.Rendering;

namespace UnityEngine.Rendering.Universal
{
    public class TransparentBloomObjPass : ScriptableRenderPass
    {
        private const int k_TotalMRTRenderTargetCount = 8;
        RenderTargetIdentifier m_CameraDepthAttachment;
        ShaderTagId m_ShaderTagId;
        ProfilingSampler m_ProfilingSampler;
        FilteringSettings m_FilteringSettings;
        string m_ProfilerTag;
        RenderTargetIdentifier[] m_CameraColorTargets = new RenderTargetIdentifier[k_TotalMRTRenderTargetCount] { 0, 0, 0, 0, 0, 0, 0, 0 };
        private RenderTargetIdentifier[] m_VaildCameraColorTargets = null;

        static int s_DistortionUVOffsetTexture = Shader.PropertyToID("_TransparentBloomObjColorTex");
        static int s_DistortionUVOffsetTexture0 = Shader.PropertyToID("_TransparentBloomTex");
        private ClearFlag[] _mrtClearFlags = new ClearFlag[2] { ClearFlag.Color, ClearFlag.Color };
        RenderTextureDescriptor descriptor;

        public TransparentBloomObjPass(string profilerTag, RenderPassEvent evt)
        {
            m_ProfilerTag = profilerTag;
            m_ShaderTagId = new ShaderTagId("TransparentBloomObj");
            renderPassEvent = evt;
            m_ProfilingSampler = new ProfilingSampler(m_ProfilerTag);
            m_FilteringSettings = new FilteringSettings(RenderQueueRange.all);
        }

        public void Setup(RenderTargetIdentifier cameraDepthAttachment)
        {
            this.m_CameraDepthAttachment = cameraDepthAttachment;
        }

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescripor)
        {
            descriptor = cameraTextureDescripor;
            descriptor.useMipMap = false;
            descriptor.autoGenerateMips = false;
            descriptor.graphicsFormat = GraphicsFormat.R16G16B16A16_SFloat;
            descriptor.depthBufferBits = 0;
            descriptor.msaaSamples = 1;

            cmd.GetTemporaryRT(s_DistortionUVOffsetTexture, descriptor, FilterMode.Bilinear);
            cmd.GetTemporaryRT(s_DistortionUVOffsetTexture0, descriptor, FilterMode.Bilinear);
            m_CameraColorTargets[0] = s_DistortionUVOffsetTexture;
            m_CameraColorTargets[1] = s_DistortionUVOffsetTexture0;     
            ConfigureTarget(m_CameraColorTargets, m_CameraDepthAttachment);

            ConfigureClear(ClearFlag.Color, Color.black);

        }


        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            var cmd = renderingData.commandBuffer;
            using (new ProfilingScope(cmd, m_ProfilingSampler))
            {
                var drawSettings = CreateDrawingSettings(m_ShaderTagId, ref renderingData, SortingCriteria.CommonTransparent);
                var filterSettings = m_FilteringSettings;
                context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings);
            }
            cmd.SetGlobalTexture(s_DistortionUVOffsetTexture, new RenderTargetIdentifier(s_DistortionUVOffsetTexture));
            cmd.SetGlobalTexture(s_DistortionUVOffsetTexture0, new RenderTargetIdentifier(s_DistortionUVOffsetTexture0));
        }


        public override void FrameCleanup(CommandBuffer cmd)
        {
            cmd.ReleaseTemporaryRT(s_DistortionUVOffsetTexture);
            cmd.ReleaseTemporaryRT(s_DistortionUVOffsetTexture0);

        }
    }
}
